#    Tichy
#
#    copyright 2008 Guillaume Chereau (charlie137@gmail.com)
#
#    This file is part of Tichy.
#
#    Tichy is free software: you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Tichy is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    General Public License for more details.
#
#    You should have received a copy of the GNU General Public License

__docformat__ = 'reStructuredText'

from tichy.item import Item
from tichy.service import Service
from tichy.base_services import TextEditService
from str import Str


class Text(Str):
    """Base class for all text in tichy

    Signals:
        "modified": emitted when the text has been modified
    """

    @property
    def title(self):
        return self

    def __init__(self, value="", editable=False):
        super(Text, self).__init__(value=value, editable=editable)

    def view(self, parent, editable=None, scroll=None, auto_keyboard=True,
             multilines=None, **kargs):
        from .gui import Button, Label, Edit, Scrollable
        editable = editable if editable is not None else self.editable

        if editable and auto_keyboard:
            button = Button(parent, border=8, **kargs)
            self.view(button, editable, scroll, False, **kargs)
            button.connect('clicked', self._on_edit)
            return button
        if scroll or scroll is None:
            ret = Scrollable(parent, 1, **kargs)
            self.view(ret, editable, False, auto_keyboard, **kargs)
            return ret
        if not editable:
            ret = Label(parent, self.value, multilines=True, **kargs)
        else:
            ret = Edit(parent, item=self, multilines=True, **kargs)

        connection = self.connect('modified', Text.on_modified, ret)
        ret.connect('destroyed', self.on_view_destroyed, connection)
        return ret

    def edit(self, parent=None, **kargs):
        """return a `Tasklet` that can be used to edit the widget"""
        # first we get the appropriate TextEdit Service
        text_edit = TextEditService.get()
        # Then we call the service with our text
        return text_edit.edit(
            parent, self, input_method=self.input_method(), **kargs)

    def _on_edit(self, button):
        text_edit = TextEditService.get()
        text_edit.edit(None, self).start()

    def on_modified(self, view):
        view.text = self.value

    def on_view_destroyed(self, view, connection):
        self.disconnect(connection)

    def create_actor(self):
        from actor import Actor
        ret = Actor(self)

        def on_edit(actor, item):
            yield item.edit()

        ret.new_action("Edit").connect('activated', on_edit)
        return ret
